package com.fractalist.man.core;

import java.util.Date;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyThreadPoolExecutor extends ThreadPoolExecutor {

	private long start;

	public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
			long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
		start = new Date().getTime();
	}

	public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
			long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
				handler);
		start = new Date().getTime();
	}

	public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
			long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue, CallerRunsPolicy callerRunsPolicy) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
				callerRunsPolicy);
		start = new Date().getTime();
	}

	public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
			long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
				threadFactory);
		start = new Date().getTime();
	}

	public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
			long keepAliveTime, TimeUnit unit,
			BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory,
			RejectedExecutionHandler handler) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
				threadFactory, handler);
		start = new Date().getTime();
	}

	@Override
	public void shutdown() {
		super.shutdown();
	}

	@Override
	protected void afterExecute(Runnable r, Throwable t) {
		// System.out.println("第" + (getCompletedTaskCount() + 1) + "次调用"
		// + ",ActiveCount:" + getActiveCount());
		if ((getCompletedTaskCount() + 1) == getTaskCount()) {
			long end = new Date().getTime();
			System.out
					.println("共" + getTaskCount() + "次执行,耗时：" + (end - start));
			this.shutdown();
		}
		if (t != null) {
			t.printStackTrace();
		}
		super.afterExecute(r, t);
	}

}
